Results 1 to 3 of 3

Thread: Floats & memory

  1. #1
    Developer Sor's Avatar
    Join Date
    Aug 2010
    Location
    The Medieval City of Bruges
    Posts
    747

    Default Floats & memory

    I know all about integer overflows, but not of floating point overflows. I noticed something odd today. Apparently I can store whatever number I want inside MoHAA's floating point datatype.
    It became evident today when I used the new pow function with the arguments 2 & 32. I was under the impression that floats were 32 bits long much like the integers in MorpheusScript but
    pow 2 64 was successfully stored in my variable and printed the exact value (except for the first 3 digits, they were 0).

    It also works if I directly define a large float:
    Code:
     local.var = 4294967296.0 //2 ^32
    , which should overflow because both integers and floats in MoH are signed.
    The greatest value I could largely fit in there was "170141183460469230000000000000000000000.000", after that it kept returning "1.#IO".
    The thing is while I can't perform addition or subtraction with these values, I can still correctly divide, multiply and compare them, which makes this 'trick' very interesting to me,
    especially since 2^32 is a value that can be quite useful.

    Can anyone explain why and/or how this is happening, and if this can cause any kind of stability issues or memory leaks?
    Morpheus Script (MoH) => You try to shoot yourself in the foot only to discover that MorpheusScript already shot your foot for you.

  2. #2
    Über Prodigy & Developer Razo[R]apiD's Avatar
    Join Date
    May 2010
    Location
    Poland, Lublin
    Posts
    3,245

    Default

    Because floats are not stored as a concrete value really.

    You can read about it here: http://en.wikipedia.org/wiki/Floatin...dern_computers

    Take a look at internal representation section. It's a bit like scientific notation.

  3. #3
    Developer Sor's Avatar
    Join Date
    Aug 2010
    Location
    The Medieval City of Bruges
    Posts
    747

    Default

    Any integer with absolute value less than or equal to 224 can be exactly represented in the single precision format, and any integer with absolute value less than or equal to 253 can be exactly represented in the double precision format. Furthermore, a wide range of powers of 2 times such a number can be represented.

    Hah, how about that, I did in fact only use multitudes and powers of 2 to test it.

    This means we can also use the infinities in scripts:
    Code:
     level.MAX_INFINITY = pow 2 128 // => "1.#IO"
    level.MIN_INFINITY = pow -2 129          // => "-1.#IO"
    Morpheus Script (MoH) => You try to shoot yourself in the foot only to discover that MorpheusScript already shot your foot for you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •